package cbfg.rvadapter.demo.state2

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import cbfg.rvadapter.DataHelper
import cbfg.rvadapter.RVAdapter
import cbfg.rvadapter.RVConcatHelper
import cbfg.rvadapter.demo.R
import cbfg.rvadapter.demo.databinding.FragmentState2Binding
import cbfg.rvadapter.demo.simple.SimpleVHFactory
import cbfg.rvadapter.entity.Person
import cbfg.rvadapter.state.RVState
import cbfg.rvadapter.state.RV_STATE_LOAD_MORE
import cbfg.rvadapter.state.RV_STATE_LOAD_MORE_ERROR
import cbfg.rvadapter.state.RV_STATE_REFRESHING
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * @author:  Tom Hawk
 * 添加时间: 2020/6/1 10:51
 * 功能描述:
 */
class State2Fragment : Fragment(), View.OnClickListener {
    private var _binding: FragmentState2Binding? = null
    private val binding: FragmentState2Binding
        get() = _binding!!

    private lateinit var rvConcatHelper: RVConcatHelper
    private lateinit var adapter: RVAdapter<Person>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentState2Binding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.btnRefreshData.setOnClickListener(this)
        binding.btnLoadMoreData.setOnClickListener(this)
        binding.btnError.setOnClickListener(this)
        binding.btnClearData.setOnClickListener(this)

        setupAdapter(view.context)
    }

    private fun setupAdapter(context: Context) {
        val vhf = State2VHF()
        val refreshingAdapter = RVAdapter<RVState>(context, vhf)
        val stateAdapter = RVAdapter<RVState>(context, vhf)
            .setItemClickListener { v, item, position ->
                if (v.id == R.id.btnRetry) {
                    binding.btnLoadMoreData.performClick()
                }
            }
        adapter = RVAdapter(context, SimpleVHFactory())
        rvConcatHelper = RVConcatHelper.Builder()
            .addRefreshAdapter(refreshingAdapter)
            .addAdapter(adapter)
            .addStateAdapter(stateAdapter)
            .build()
            .bind(binding.rvList)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btnRefreshData -> {
                rvConcatHelper.showState(RV_STATE_REFRESHING)
                lifecycleScope.launch {
                    delay(2000)
                    rvConcatHelper.hideState()
                    adapter.setItems(DataHelper.getPeople())
                    rvConcatHelper.enableShowEmptyState = true
                }
            }

            R.id.btnLoadMoreData -> {
                rvConcatHelper.showState(RV_STATE_LOAD_MORE)
                lifecycleScope.launch {
                    delay(2000)
                    rvConcatHelper.hideState()
                    adapter.addList(list = DataHelper.getPeople())
                }
            }

            R.id.btnError -> {
                rvConcatHelper.showState(RV_STATE_LOAD_MORE)
                lifecycleScope.launch {
                    delay(2000)
                    rvConcatHelper.showState(RV_STATE_LOAD_MORE_ERROR)
                }
            }

            R.id.btnClearData -> {
                rvConcatHelper.hideState()
                adapter.clear()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}